
$(function(){
	var userName = sessionStorage['username'];
	if(userName==null||userName==""){
		$(window).mobileMsgBox({msgText:"请先登录",delayTime:1000});
		window.location="login.html#loginPage";
	}
	$("#home").bind("tap",toHome);
	$("#gridPage").mobileGrid({pageSize:5,currentPageIndex:1,userId:sessionStorage['userId'],server:localStorage['APP_AJAX_PATH']+'/drinkingBar/view/shopCart.jsp.findShopCartByUserId'});
	$(".checkbox").attr("onclick","setTotal()");
	$("#total").html(0);
	 //确认
	$("#deleteShopCart").bind("tap",function(){
		var rows=$("#gridPage").selectedRecords();
		 if (!rows) {
			 $(window).mobileMsgBox({msgText:"请选择一行",delayTime:500});
				return;
			    }
			for(var i=0;i<rows.length;i++){
			    var params ={};
			    params["selectId"]=rows[i]["id"];
			    Newtouch.Ajax.sendParams(params, localStorage['APP_AJAX_PATH']+'/drinkingBar/view/shopCart.jsp.deleteShopCart', function(data){
			    	$(window).mobileMsgBox({msgText:"删除成功",delayTime:500});
 	                window.location="shopCart.html";
			   	 },function(err){
			   		$(window).mobileMsgBox({msgText:"删除失败",delayTime:500});
			   	 });
			 }
	  	
   });
	$("#buyShopCart").bind("tap",function(){
		var rows=$("#gridPage").selectedRecords();
		 if (!rows) {
			 $(window).mobileMsgBox({msgText:"请选择一行",delayTime:500});
			 $('#min').attr('style',"display:none");
			    }
		 	var total = 0;
		 	var shopCartIds = "";
		 	var productIds = "";
		 	var counts = "";
			for(var i=0;i<rows.length;i++){
			    var price = rows[i]["product.price"];
			    var count = $("#text_box"+rows[i]["id"]).val();
			    total =total + price*count;
			    $("#total").html(total);
			    productIds=rows[i]['product.id']+':'+productIds;
			    shopCartIds=rows[i]['id']+':'+shopCartIds;
			    var productName = rows[i]['product.name'];
			    sessionStorage.setItem(productName, $("#text_box"+rows[i]["id"]).val());
			    sessionStorage.setItem('shopCartIds', shopCartIds);
			    sessionStorage.setItem('total', total);
			    sessionStorage.setItem('productIds', productIds);
			    window.location="buyShopCart.html";
			    
			 }
	  	
   });
	
});
function toHome(){
	window.location = "main.html";
}
function setTotal(){
 	var total = 0;
	for(var i=0;i<rows.length;i++){
	    var price = rows[i]["product.price"];
	    var count = $("#text_box"+rows[i]["id"]).val();
	    total =total + price*count;
	    $("#total").html(total);
	 }
	
}
function name(cell,data){
	var html=''+data['product.name']+'';
	cell.html(html);
}
function price(cell,data){
	var html=''+data['product.price']+'';
	cell.html(html);
}
function quantity(cell,data){
	var html='<img src="../image/minus_easyicon_net_16.png"  id="min" onclick=setMin('+data['product.price']+','+data.id+')><input id="text_box'+data.id+'" type="text" style="margin-bottom: 0px;width:20px;border-radius: 0px; padding: 2px 6px;" value="'+data.quantity+'"></input><img src="../image/plus_easyicon_net_16.png"   id="add" onclick=setMax('+data['product.price']+','+data.id+')>';
	cell.html(html);
}
function sum(cell,data){
	var html='<div id="sum_'+data.id+'">'+data['product.price']*data.quantity+'</div>';
	cell.html(html);
}
var androidLocal=function(p){
	for(var k in p){
		sessionStorage[k]=p[k];
	}
	
}

function setMin(price,id){
	$("#text_box"+id).val(parseInt($("#text_box"+id).val())-1);
	        if (parseInt($("#text_box"+id).val())<=1){	
	        	$(window).mobileMsgBox({msgText:"数量不能再减啦！",delayTime:500});		
	        	$("#text_box"+id).val(1);
	        }
	setTotal(price,id);
}
function setMax(price,id){
	$("#text_box"+id).val(parseInt($("#text_box"+id).val())+1);

	setTotal(price,id);
}
function setTotal(prcie,id){
	$("#sum_"+id).html(prcie*$("#text_box"+id).val());
}

	
	
